package com.classtimer.Tasks;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.classtimer.Model.StudentInfo;

public class SendEmailJob implements Runnable{



	@Override
	public void run() {
		// TODO Auto-generated method stub
		if(sendEmail()) {
			if(sendIPEmail()) {
				System.out.println("邮件发送成功");
				return;
			}else {
				sendIPEmail();
			}
			System.out.println("邮件发送成功");
			return;
		}else {
			sendEmail();
		}
	      
	}

	public boolean sendEmail() {
		
		boolean flag  = true;
		  // 收件人电子邮箱
		  String to = "yank.tenyond@gmail.com";

	      // 发件人电子邮箱
	      String from = "3465961097@qq.com";

	      // 指定发送邮件的主机为 smtp.qq.com
	      String host = "smtp.qq.com";  //QQ 邮件服务器

	      // 获取系统属性
	      Properties properties = System.getProperties();

	      // 设置邮件服务器
	      properties.setProperty("mail.smtp.host", host);

	      //用户密码认证
	      properties.put("mail.smtp.auth", "true");

	      //配置SSL加密方式
	      properties.put("mail.smtp.port", "587");

	      //调试模式
	      //properties.put("mail.debug", "true");

	      // 获取默认session对象
	      Session session = Session.getDefaultInstance(properties,new Authenticator(){
	        public PasswordAuthentication getPasswordAuthentication()
	        {
	         return new PasswordAuthentication("3465961097@qq.com", "inmwbdrxvssydbad"); //发件人邮件用户名、密码
	        }
	       });

	      try{
	         // 创建默认的 MimeMessage 对象
	         MimeMessage message = new MimeMessage(session);

	         // Set From: 头部头字段
	         message.setFrom(new InternetAddress(from));

	         // Set To: 头部头字段
	         message.addRecipient(Message.RecipientType.TO,
	                                  new InternetAddress(to));
	         
	         List<String> email = getEmails();
	         if(email != null) {
	        	 Address[] ccAdresses = new InternetAddress[email.size()];
	        	 for (int i=0; i<email.size(); i++){
	                    ccAdresses[i] = new InternetAddress(email.get(i));
	             }
	        	 message.setRecipients(Message.RecipientType.CC, ccAdresses);
	         }

	         // Set Subject: 头部头字段
	         message.setSubject("软件1705防堕落系统周报表");

	         
	         // 设置消息体
	         message.setContent(getContent(), "text/html;charset=UTF-8");

	         // 发送消息
	         Transport.send(message);
	         
	      }catch (MessagingException mex) {
	         mex.printStackTrace();
	         flag = false;
	      }
	      return flag;
	}
	
	public String getContent() {
		DecimalFormat df = new DecimalFormat("0.00");
		List<StudentInfo> students = StudentInfo.dao.find("select * from student_info where id > 0");
		String content = "";
		String base = "<!DOCTYPE html>\r\n" + 
				"<html lang=\"en\">\r\n" + 
				"<head>\r\n" + 
				"    <meta charset=\"UTF-8\">\r\n" + 
				"    <title>软件1705防堕落系统   数据报表</title>\r\n" + 
				"</head>\r\n" + 
				"<body>\r\n" + 
				"    <h1>软件1705防堕落系统  数据报表 (<a href=\"http://localhost:8188\">系统入口</a>)</h1>\r\n" + 
				"    <h4>该邮件由防堕落系统后台机器人默认发送，如有问题联系你亲爱的导生大大</h4>\r\n" + 
				"    <font size=\"5\" color=\"red\">是的，你亲爱的导生大大的确拿你没办法，但是希望红色的同学主动联系我，不要等我来联系你</font>\r\n" + 
				"    <h4>表格描述：绿色（合格）—————  红色（警告）（部分邮件系统解析错误，不能识别颜色）</h4>\r\n" + 
				"    <table border='0' cellpadding='2' cellspacing='0' topmargin='0' leftmargin='0' marginwidth='0' marginheight='0'>\r\n" + 
				"		<tr>\r\n" + 
				"			<td bgcolor='green' align='center' width='100' height='25'><b>序号</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
				"			<td bgcolor='green' align='center' width='100' height='25'><b>姓名</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
				"			<td bgcolor='green' align='center' width='100' height='25'><b>班级</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
				"			<td bgcolor='green' align='center' width='200' height='25'><b>上周总在线时长(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
				"			<td bgcolor='green' align='center' width='200' height='25'><b>上周平均在线时长(小时)</b></td>\r\n" + 
				"		</tr>";
		String divider ="<tr>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"		</tr>";
		content+=divider;
		content +=base;
		content+=divider;
		String laString="</table>\r\n" + 
				"</body>\r\n" + 
				"</html>";
		for(int i=0;i<students.size();i++) {
			String item = "";
			double average = students.get(i).getWeeksum()/7;
			if(students.get(i).getWeeksum()!=0) {				
				item = "<tr>\r\n" + 
						"			<td bgcolor='green' align='center' width='100' height='25'><b>"+(i+1)+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='green' align='center' width='100' height='25'><b>"+students.get(i).getName()+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='green' align='center' width='100' height='25'><b>软件1705</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='green' align='center' width='200' height='25'><b>"+df.format(students.get(i).getWeeksum())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='green' align='center' width='200' height='25'><b>"+df.format(average)+"</b></td>\r\n" + 
						"		</tr>";
			}else {
				item = "<tr>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='25'><b>"+(i+1)+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='25'><b>"+students.get(i).getName()+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='25'><b>软件1705</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='25'><b>"+df.format(students.get(i).getWeeksum())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='25'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='25'><b>"+df.format(average)+"</b></td>\r\n" + 
						"		</tr>";
			}
			content+=item;
			content+=divider;
			
		}
		content += laString;
		return content;
	}
	
	public boolean sendIPEmail() {
			
			boolean flag  = true;
			  // 收件人电子邮箱
			  String to = "yank.tenyond@gmail.com";
	
		      // 发件人电子邮箱
		      String from = "3465961097@qq.com";
	
		      // 指定发送邮件的主机为 smtp.qq.com
		      String host = "smtp.qq.com";  //QQ 邮件服务器
	
		      // 获取系统属性
		      Properties properties = System.getProperties();
	
		      // 设置邮件服务器
		      properties.setProperty("mail.smtp.host", host);
	
		      //用户密码认证
		      properties.put("mail.smtp.auth", "true");
	
		      //配置SSL加密方式
		      properties.put("mail.smtp.port", "587");
	
		      //调试模式
		      //properties.put("mail.debug", "true");
	
		      // 获取默认session对象
		      Session session = Session.getDefaultInstance(properties,new Authenticator(){
		        public PasswordAuthentication getPasswordAuthentication()
		        {
		         return new PasswordAuthentication("3465961097@qq.com", "kmezngxvewfzdbac"); //发件人邮件用户名、密码
		        }
		       });
	
		      try{
		         // 创建默认的 MimeMessage 对象
		         MimeMessage message = new MimeMessage(session);
	
		         // Set From: 头部头字段
		         message.setFrom(new InternetAddress(from));
	
		         // Set To: 头部头字段
		         message.addRecipient(Message.RecipientType.TO,
		                                  new InternetAddress(to));
		         
		         List<String> email = new ArrayList<String>();
		         email.add("dreamcreationman@outlook.com");
		         if(email != null) {
		        	 Address[] ccAdresses = new InternetAddress[email.size()];
		        	 for (int i=0; i<email.size(); i++){
		                    ccAdresses[i] = new InternetAddress(email.get(i));
		             }
		        	 message.setRecipients(Message.RecipientType.CC, ccAdresses);
		         }
	
		         // Set Subject: 头部头字段
		         message.setSubject("软件1705防堕落系统周报表");
	
		         
		         // 设置消息体
		         message.setContent(getIPContent(), "text/html;charset=UTF-8");
	
		         // 发送消息
		         Transport.send(message);
		         
		      }catch (MessagingException mex) {
		         mex.printStackTrace();
		         flag = false;
		      }
		      return flag;
		}
	
	public String getIPContent() {
		DecimalFormat dfs = new DecimalFormat("0.00");
		List<StudentInfo> students = StudentInfo.dao.find("select * from student_info where id > 0");
		String content = "";
		String base = "<!DOCTYPE html>\r\n" + 
				"<html lang=\"en\">\r\n" + 
				"<head>\r\n" + 
				"    <meta charset=\"UTF-8\">\r\n" + 
				"    <title>软件1705防堕落系统   数据报表</title>\r\n" + 
				"</head>\r\n" + 
				"<body>\r\n" + 
				"    <h1>软件1705防堕落系统  数据报表 (<a href=\"http://localhost:8188\">系统入口</a>)</h1>\r\n" + 
				"    <h4>该邮件由防堕落系统后台机器人默认发送，如有问题联系你亲爱的导生大大</h4>\r\n" + 
				"    <font size=\"5\" color=\"red\">是的，你亲爱的导生大大的确拿你没办法，但是希望红色的同学主动联系我，不要等我来联系你</font>\r\n" + 
				"    <h6>表格描述：绿色（合格）————————  红色（警告）</h6>\r\n" + 
				"    <table border='0' cellpadding='2' cellspacing='0' topmargin='0' leftmargin='0' marginwidth='0' marginheight='0'>\r\n" + 
				"		<tr>\r\n" + 
				"			<td bgcolor='#green' align='center' width='100' height='18'><b>序号</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='100' height='18'><b>姓名</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='100' height='18'><b>班级</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周一(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周二(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周三(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周四(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周五(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周六(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>周日(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>上周总在线时长(小时)</b></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
				"			<td bgcolor='#green' align='center' width='200' height='18'><b>上周平均在线时长(小时)</b></td>\r\n" + 
				"		</tr>";
		String divider ="<tr>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='100' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='0.5' height='0.5'></td>\r\n" + 
				"			<td bgcolor='yellow' align='center' width='200' height='0.5'></td>\r\n" + 
				"		</tr>";
		content+=divider;
		content +=base;
		content+=divider;
		String laString="</table>\r\n" + 
				"</body>\r\n" + 
				"</html>";
		for(int i=0;i<students.size();i++) {
			String item = "";
			double average = students.get(i).getWeeksum()/7;
			if(students.get(i).getWeeksum()!=0) {				
				item = "<tr>\r\n" + 
						"			<td bgcolor='#green' align='center' width='100' height='18'><b>"+(i+1)+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='100' height='18'><b>"+students.get(i).getName()+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='100' height='18'><b>软件1705</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getMonday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getTuesday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getWednesday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getThursday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getFriday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getSaturday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getSunday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getWeeksum())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='#green' align='center' width='200' height='18'><b>"+dfs.format(average)+"</b></td>\r\n" + 
						"		</tr>";
			}else {
				item = "<tr>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='18'><b>"+(i+1)+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='18'><b>"+students.get(i).getName()+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='100' height='18'><b>软件1705</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getMonday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getTuesday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getWednesday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getThursday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getFriday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getSaturday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getSunday())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(students.get(i).getWeeksum())+"</b></td>\r\n" + 
						"			<td bgcolor='yellow' align='center' width='0.5' height='18'></td>\r\n" + 
						"			<td bgcolor='red' align='center' width='200' height='18'><b>"+dfs.format(average)+"</b></td>\r\n" + 
						"		</tr>";
			}
			content+=item;
			content+=divider;
			
		}
		content += laString;
		return content;
	}
	
	public List<String> getEmails() {
		List<StudentInfo> students = StudentInfo.dao.find("select * from student_info where id > 0");
	    List<String> emails = new ArrayList<String>();
	    for (StudentInfo studentInfo : students) {
			emails.add(studentInfo.getEmail());
	    }
		return emails;
		
	}

}
